package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.entity.shop.Category;
import cn.enilu.flash.bean.entity.shop.CategoryFirst;
import cn.enilu.flash.bean.entity.shop.Goods;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.shop.CategoryFirstVO;
import cn.enilu.flash.service.shop.CategoryFirstService;
import cn.enilu.flash.service.shop.CategoryService;
import cn.enilu.flash.service.shop.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/categoryFirst")
public class FirstCategoryController {
    @Autowired
    private CategoryFirstService categoryFirstService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Object list() {
        List<CategoryFirst> categoryFirstList = categoryFirstService.queryAll(SearchFilter.build("flag","1"));
        List<CategoryFirstVO> categoryFirstVOList = new ArrayList<>();
        categoryFirstList.forEach(l->{
            CategoryFirstVO categoryFirstVO = new CategoryFirstVO();
            List<Category> categoryList = categoryService.queryAll(SearchFilter.build("fistId",l.getId()), Sort.by(Sort.Direction.ASC,"createTime"));
            categoryFirstVO.setName(l.getName());
            categoryFirstVO.setCategoryList(categoryList);
            categoryFirstVOList.add(categoryFirstVO);
        });
        return categoryFirstVOList;
    }

    @RequestMapping(value = "/getGoodsListByCategoryId",method = RequestMethod.POST)
    public Object getGoodsListByCategoryId(String sing,@RequestParam(value = "categoryId") String categoryId){
        Sort sort = Sort.by(Sort.Direction.ASC,"id");
        sing = sing == null ? "0": sing;
        if(sing.equals("1")){
             sort = Sort.by(Sort.Direction.DESC,"createTime");
        }
        if(sing.equals("2")){
             sort = Sort.by(Sort.Direction.DESC,"isHot").and(Sort.by(Sort.Direction.DESC,"modifyTime"));
        }
        if(sing.equals("3")){
             sort = Sort.by(Sort.Direction.DESC,"price");
        }
        if(sing.equals("4")){
             sort = Sort.by(Sort.Direction.ASC,"price");
        }
        List<SearchFilter> searchFilterList = new ArrayList<>();
        searchFilterList.add(SearchFilter.build("idCategory",categoryId));
        searchFilterList.add(SearchFilter.build("isOnSale",true));
        searchFilterList.add(SearchFilter.build("isDelete",false));
        searchFilterList.add(SearchFilter.build("isAudit",1));
        List<Goods> goodsList = goodsService.queryAll(searchFilterList,sort);
        return goodsList;
    }
}
